package StudentAchievementManagementSystem.component;

import StudentAchievementManagementSystem.mysql.MysqlUtil;
import StudentAchievementManagementSystem.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AddStuGradeDialog extends JDialog {
    final int WIDTH =350;
    final int HEIGHT=200;

    public AddStuGradeDialog(JFrame jf, String title, Boolean isModel){
        super(jf,title,isModel);
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装学号
        Box snoBox = Box.createHorizontalBox();
        JLabel snoLabel = new JLabel("学号");
        JTextField snoTextField = new JTextField(15);

        snoBox.add(snoLabel);
        snoBox.add(Box.createHorizontalStrut(20));
        snoBox.add(snoTextField);

        //组装课程号
        Box cnoBox = Box.createHorizontalBox();
        JLabel cnoLabel = new JLabel("课程号");
        JTextField cnoTextField = new JTextField(15);

        cnoBox.add(cnoLabel);
        cnoBox.add(Box.createHorizontalStrut(20));
        cnoBox.add(cnoTextField);

        //组装成绩
        Box gradeBox = Box.createHorizontalBox();
        JLabel gradeLabel = new JLabel("成绩");
        JTextField gradeTextField = new JTextField(15);

        gradeBox.add(gradeLabel);
        gradeBox.add(Box.createHorizontalStrut(20));
        gradeBox.add(gradeTextField);

        //组装添加按钮
        Box btnBox = Box.createHorizontalBox();
        JButton button = new JButton("添加");
        btnBox.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sno = snoTextField.getText().trim();
                String cno = cnoTextField.getText().trim();
                String  grade  = gradeTextField.getText().trim();

//                String sql="select ";
                Connection connection = null;
                try {
                    connection = MysqlUtil.getConnection();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                Statement statement = null;
                try {
                    statement = connection.createStatement();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                //判断表中是否有sno，cno数据
                String Sqltest="select sno from studentcoursegrade where sno='"+sno+"' and cno='"+cno+"' ";
                try {
                    ResultSet resultSet1 = statement.executeQuery(Sqltest);
                    if (!resultSet1.next()){
                        //添加的sql语句
                        String sql="insert into studentcoursegrade(sno,cno,grade) select '"+sno+"','"+cno+"','"+grade+"' from dual where exists(select sno from student where sno='"+sno+"') and exists(select cno from course where cno='"+cno+"')and not exists(select sno,cno  from studentcoursegrade where cno='"+cno+"'and sno ='"+sno+"')";
                        try {
                            statement.execute(sql);
//                    String Sqltest="select sno from studentcoursegrade where sno='"+sno+"' and cno='"+cno+"' ";
                            //确定有数据，判断是否成功删除
                            ResultSet resultSet2 = statement.executeQuery(Sqltest);
                            if(resultSet2.next()){
                                JOptionPane.showMessageDialog(jf,"添加成功");
                                dispose();
                            }else{
                                JOptionPane.showMessageDialog(jf,"添加失败");
                            }
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }else{JOptionPane.showMessageDialog(jf,"已有与输入相同sno，cno数据！");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }





            }
        });


        vBox.add(Box.createVerticalStrut(20));
        vBox.add(snoBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(cnoBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(gradeBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);

        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(20));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(20));

        this.add(hBox);
    }




}
